package arrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Test2 {

    public static List<Card> cards = new ArrayList<>();
    private static final String[] suits = {"♥","♠","♦","♣"};

    // 买牌
    public static List<Card> buyCard(){
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                cards.add(new Card(suits[i],j));
            }
        }
        return cards;
    }
    // 洗牌
    public static void washCards(){
        for (int i = cards.size() - 1; i > 0; i--) {
            Random random = new Random();
            int rand = random.nextInt(i);// 生成 0 ~ i 的随机数,每个都和前面的其中一个交换
            swap(i, rand);
        }
    }
    private static void swap(int i, int j) {
        Card card = cards.get(i);
        cards.set(i,cards.get(j));
        cards.set(j,card);
    }

    public static void main(String[] args) {

        List<Card> cards = buyCard();
        System.out.println("买牌:" + cards);
        washCards();
        System.out.println("洗牌:" + cards);

        List<List<Card>> lists = new ArrayList<>();
        // 揭牌
        List<Card> list1 = new ArrayList<>();
        List<Card> list2 = new ArrayList<>();
        List<Card> list3 = new ArrayList<>();
        lists.add(list1);
        lists.add(list2);
        lists.add(list3);

        while (!cards.isEmpty()) {
            for (int i = 0; i < 3; i++) {
                if (!cards.isEmpty()) {
                    lists.get(i).add(cards.remove(0));
                }
            }
        }

        System.out.println("第1个人的牌: " + list1);
        System.out.println("第2个人的牌: " + list2);
        System.out.println("第3个人的牌: " + list3);
    }
}


class Card {
    public int rank; // 牌面值
    public String suit; // 花色

    public Card( String suit,int rank) {
        this.rank = rank;
        this.suit = suit;
    }

    @Override
    public String toString() {
        return "[" + suit + " " + rank + "]";
    }
}